﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// GiftUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class GiftUserControl : UserControl, IPlugin
    {
        private GiftService giftService = ServiceContainer.GetService<GiftService>();
        private IList<Gift> gifts = new List<Gift>();

        public GiftUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "礼品管理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }


        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataCollection<Gift> datas = this.giftService.GetPageByMap(new Dictionary<string, object>(), 0, 0);
                this.gifts = datas.Datas;
                this.lstGift.ItemsSource = this.gifts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new GiftEditWindow();
                bool? ret = window.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    return;
                }
                this.gifts.Add(window.Gift);
                this.lstGift.ItemsSource = null;
                this.lstGift.ItemsSource = this.gifts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                Gift gift = btn.DataContext as Gift;
                var window = new GiftEditWindow { Gift = gift };
                bool? ret = window.ShowDialog();
                if (ret == null || ret.Value == false)
                {
                    return;
                }
                this.lstGift.ItemsSource = null;
                this.lstGift.ItemsSource = this.gifts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                Gift gift = btn.DataContext as Gift;
                if (MessageBox.Show(gift.Name, "是否要删除", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.giftService.Delete(gift);
                this.gifts.Remove(gift);
                this.lstGift.ItemsSource = null;
                this.lstGift.ItemsSource = this.gifts;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
